package com.itoffer.service.impl;

import com.itoffer.mapper.ResumeMapper;
import com.itoffer.pojo.ResumeBasicInfo;
import com.itoffer.service.ResumeService;
import com.itoffer.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class ResumeServiceImpl implements ResumeService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    @Override
    public int addResume(ResumeBasicInfo basicInfo, int applicant_id) {
        SqlSession sqlSession = factory.openSession();
        ResumeMapper mapper = sqlSession.getMapper(ResumeMapper.class);
        int i = mapper.addResume(basicInfo, applicant_id);
       // System.out.println("业务层"+i+"行受影响");
        //提交事务
        sqlSession.commit();
        sqlSession.close();

        return i;
    }

    @Override
    public boolean selectResumeByApplicant_id(int applicant_id) {
        SqlSession sqlSession = factory.openSession();
        ResumeMapper mapper = sqlSession.getMapper(ResumeMapper.class);
        Integer i = mapper.selectResumeByApplicant_id(applicant_id);
        sqlSession.close();
        if(i==null){
            return false;
        }
        return i>0;
    }

    @Override
    public ResumeBasicInfo selectCurrentResume(int basicinfo_id) {

        SqlSession sqlSession = factory.openSession();
        //获取mapper对象
        ResumeMapper mapper = sqlSession.getMapper(ResumeMapper.class);
        //返回一个结果对象
        ResumeBasicInfo basicInfo = mapper.selectCurrentResume(basicinfo_id);

        sqlSession.close();

        return basicInfo;
    }

    @Override
    public int selectBasicinfo_idByApplicant_id(int applicant_id) {
        SqlSession sqlSession = factory.openSession();
        //获取mapper对象
        ResumeMapper mapper = sqlSession.getMapper(ResumeMapper.class);
        //返回一个结果
        Integer basicinfo_id = mapper.selectBasicinfo_idByApplicant_id(applicant_id);


        sqlSession.close();

        return basicinfo_id;
    }


    @Override
    public void updatePicPath(int basicinfo_id, String newFileName) {
        SqlSession sqlSession = factory.openSession();
        ResumeMapper mapper = sqlSession.getMapper(ResumeMapper.class);
        mapper.updatePicPath(basicinfo_id, newFileName);
        sqlSession.commit();
        sqlSession.close();
    }

    public boolean updateResume(ResumeBasicInfo basicInfo){
        SqlSession sqlSession = factory.openSession();
        ResumeMapper mapper = sqlSession.getMapper(ResumeMapper.class);
        int i = mapper.updateResume(basicInfo);
        sqlSession.commit();
        sqlSession.close();
        return i>0;
    }
}
